package vlc;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;

public class StreamContext {
	private static StreamContext instance;
	private String streamUrl;
	private String media;
	private HeadlessMediaPlayer mediaPlayer;
	private String options;
	private String videoSource;
	
	public synchronized static StreamContext getInstance() {
		if (instance == null) {
			instance = new StreamContext();
		}
		return instance;
	}

	/**Play HeadlessMediaPlayer with generated option*/
	
	public void startStream() throws InterruptedException {

			options = ":sout=#"+options;
	
			/*Duplicate when multiple stream*/
			//options = ":sout=#duplicate{" +
			//		"dst=\"transcode{vcodec=theo,venc=theora{quality=3},scale=1,acodec=vorb,ab=160,channels=2,samplerate=44100}:http{dst=" + streamUrl + "/media.ogv}\"" +
			//		",dst=\"transcode" +
			//		"{vcodec=VP80,vb=800,scale=0,acodec=vorb,ab=128,channels=2,samplerate=44100}" +
			//		":http{mux=ffmpeg{mux=webm},dst=" + streamUrl+"/media.webm}\"" +
			//		"}";
		

		//String	 options = ":sout=#transcode{vcodec=VP80,vb=800,scale=0,acodec=vorb,ab=128,channels=2,samplerate=44100}:http{mux=ffmpeg{mux=webm},dst=" + streamUrl+"/media.webm}";
		System.out.println("Streaming '" + media + "' to '" + options + "'");

		MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(options);
		mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
		mediaPlayer.playMedia(media, options);
	}

	/** Stop MediaPlayer*/
	public void stopStream() {
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
		}
	}

	/**
	 * Genelate appropriate HTML5 Video code to play stream
	 * @param width Width value in main.jps
	 * @param height Height value in main.jps
	 * @return HTML code to implement in Web page to play media  
	 */
	public String getVideoComponent(String width, String height) {
		StringBuffer sb = new StringBuffer("<video poster='apple-touch-icon-144x144-precomposed.png' autoplay loop controls tabindex='0'");
		sb.append(" width='" + width + "'");
		sb.append(" height='" + height + "'");
		sb.append(">");
		sb.append(videoSource);
		sb.append("<p>Your browser can't play any video.</p>");
		sb.append("</video>");
		System.out.println(sb.toString());
		return sb.toString();
	}

	public boolean isPlaying() {
		if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
			return false;
		}
		return true;
	}

	public String getStreamUrl() {
		return streamUrl;
	}

	public void setStreamUrl(String url) {
		this.streamUrl = url;
	}

	public String getMrl() {
		return media;
	}

	public void setMrl(String mrl) {
		this.media = mrl;
	}


	/**
	 * Set video codec to appropriate Web browser.
	 * @param codecName Input from Administrative tool.
	 * Theora : FireFox, Opera
	 * WebM : Chrome, FireFox, Opera
	 * h264 : IE, Safari
	 */
	public void setCodec(String codecName) {
		if(codecName.equals("Theora")){
			options = "transcode{vcodec=theo,venc=theora{quality=9},scale=0}:http{dst=" + streamUrl + "/media.ogv}";
			//options = ":sout=#transcode{vcodec=theo,venc=theora{quality=1},scale=1,acodec=vorb,ab=160,channels=2,samplerate=44100}:http{dst=" + streamUrl + "/media.ogv}";
			videoSource = "<source src='http://" + streamUrl + "/media.ogv' type='video/ogg; codecs='theora, vorbis' />";
		}else if(codecName.equals("WebM")){
			options = "transcode{vcodec=VP80,vb=800,scale=0}:http{mux=ffmpeg{mux=webm},dst=" + streamUrl+"/media.webm},select=noaudio";
			videoSource="<source src='http://" + streamUrl + "/media.webm'  type=\"video/webm\" />";
			
		}else {//if(codecName.equals("H264")){
			options = "transcode{vcodec=h264,scale=0}:std{access=http,mux=ts,dst=" + streamUrl+"/media.mp4}";
			videoSource="<source src='http://" + streamUrl + "/media.mp4'  type=\"video/mp4\" />";
		}
	}
}
